package kau.dao;

import java.util.ArrayList;
import java.util.HashMap;

import kau.common.DBUtil;
import kau.exception.DuplicationException;
import kau.exception.RecordNotFoundException;
import kau.vo.Registeration;

import org.apache.ibatis.session.SqlSession;

public class RegisterationDAOImpl implements RegisterationDAOIF {

	DBUtil db = null;
	private static RegisterationDAOImpl instance;

	private RegisterationDAOImpl() {
		db = DBUtil.getInstance();
	}

	public static RegisterationDAOImpl getInstance() {
		if (instance == null)
			instance = new RegisterationDAOImpl();
		return instance;
	}

	public boolean isExist(Registeration registeration) {
		SqlSession session = db.getSession();
		boolean flag = false;
		String result = null;
		try {
			result = session.selectOne("dbConfig.RegisterationMapper.isExist",
					registeration);
		} finally {
			session.close();
		}
		if (result != null)
			flag = true;

		return flag;
	}

	@Override
	public void insert(Registeration registeration) throws DuplicationException {
		if (isExist(registeration))
			throw new DuplicationException(registeration);
		SqlSession session = db.getSession();
		try {
			session.insert("dbConfig.RegisterationMapper.insert", registeration);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}

	}

	@Override
	public void update(Registeration registeration)
			throws RecordNotFoundException {
		if (!isExist(registeration))
			throw new RecordNotFoundException(registeration);
		SqlSession session = db.getSession();

		try {
			session.update("dbConfig.RegisterationMapper.update", registeration);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}

		// delete(registeration);
		//
		// SqlSession session = db.getSession();
		// insert(new Registeration(registeration.getCustId(), courseId,
		// isConfirm));
		// 수형이형한테 물어볼것.. 내가 왜이렇게 하자고햇지? 이해가안간다...왜그랫을까

	}

	@Override
	public void delete(Registeration registeration)
			throws RecordNotFoundException {
		if (!isExist(registeration))
			throw new RecordNotFoundException(registeration);

		SqlSession session = db.getSession();
		try {
			session.delete("dbConfig.RegisterationMapper.delete", registeration);
			session.commit();
		} finally {
			if (session != null)
				session.close();
		}

	}

	/*
	 * @Override public Registeration getRegisteration(Registeration
	 * registeration) throws RecordNotFoundException {
	 * if(!isExist(registeration)) throw new
	 * RecordNotFoundException(registeration); SqlSession session =
	 * db.getSession(); Registeration registeration_ret=null; try{
	 * registeration_ret =
	 * session.selectOne("dbConfig.RegisterationMapper.getRegisteration",
	 * registeration); }finally{ session.close(); }
	 * 
	 * return registeration_ret; }
	 */

	@Override
	public ArrayList<Registeration> getAllRegisterations() {
		ArrayList<Registeration> registerationList = new ArrayList<Registeration>();
		SqlSession session = db.getSession();
		try {
			registerationList = (ArrayList) session
					.selectList("dbConfig.RegisterationMapper.getAllRegisterations");
		} finally {
			if (session != null)
				session.close();
		}

		return registerationList;
	}

	@Override
	public ArrayList<Registeration> getAllCourseId(String custId) {
		ArrayList<Registeration> registerationList = new ArrayList<Registeration>();
		SqlSession session = db.getSession();
		try {
			registerationList = (ArrayList) session.selectList(
					"dbConfig.RegisterationMapper.getAllcourseId", custId);
		} finally {
			if (session != null)
				session.close();
		}

		return registerationList;
	}

	@Override
	public ArrayList<Registeration> getAllCustId(int courseId) {
		ArrayList<Registeration> registerationList = new ArrayList<Registeration>();
		SqlSession session = db.getSession();
		try {
			registerationList = (ArrayList) session.selectList(
					"dbConfig.RegisterationMapper.getAllCustId", courseId);
		} finally {
			if (session != null)
				session.close();
		}

		return registerationList;
	}

	public Registeration getRegisteration(String custId, int courseId){
		Registeration registInfo = new Registeration();
	
		SqlSession session = db.getSession();
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("custId", custId);
		param.put("courseId", courseId);
		
		try{
			registInfo = session.selectOne("dbConfig.RegisterationMapper.getRegisteration", param);
		}finally{
			if(session != null)
				session.close();
		}

		return registInfo; 
	}

	@Override
	public ArrayList<Registeration> getAllConfirmCustId(int courseId) {
		// TODO Auto-generated method stub
		ArrayList<Registeration> registerationList = new ArrayList<Registeration>();
		SqlSession session = db.getSession();
		try {
			registerationList = (ArrayList) session.selectList(
					"dbConfig.RegisterationMapper.getAllConfirmCustId", courseId);
		} finally {
			if (session != null)
				session.close();
		}

		return registerationList;
	}
}
